{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import numpy as np \n",
    "import pandas as pd\n",
    "import os,sys,argparse\n",
    "import re\n",
    "\n",
    "def ptm_cdr_wseq(seq,match_pattern):\n",
    "\n",
    "    '''\n",
    "    Cdrs\n",
    "    AA such like \"NG\" \"NS\" \"DG\" on CDRs ;PTM means post translational modification.\n",
    "    match_patten = r'N[GS]|DG'\n",
    "\n",
    "\n",
    "    Wholeseq\n",
    "    AA such like \"N?S\" \"N?T\" on the whole clone sequence.\n",
    "    match_patten = r'N.[ST]'\n",
    "    '''\n",
    "    \n",
    "    match = re.findall(match_pattern,seq)\n",
    "    count = len(match)\n",
    "\n",
    "\n",
    "    return count\n",
    "\n",
    "\n",
    "def cal_ptms(line):\n",
    "    \n",
    "    cdr1counts = ptm_cdr_wseq(line[0],r'N[GS]|DG')\n",
    "    cdr2counts = ptm_cdr_wseq(line[1],r'N[GS]|DG')\n",
    "    cdr3counts = ptm_cdr_wseq(line[2],r'N[GS]|DG')\n",
    "    wseqcounts = ptm_cdr_wseq(line[3],r'N.[ST]')\n",
    "    counts = cdr1counts + cdr2counts + cdr3counts + wseqcounts\n",
    "    \n",
    "    return  cdr1counts,cdr2counts,cdr3counts,wseqcounts,counts\n",
    "  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "    \n",
    "df_merge_clone = pd.DataFrame([['GFTFSRYS','ITSSSRYI','CARDPACGDYVYFDYW','771'],\\\n",
    "['NGNGCCG','ITTTTTNGSNGNGNGT','AATCITGSNGSECTW','589'],['NGNCCGT','ITTTTTNGSNGNGN\\\n",
    "GT','AATCITGWGNGSECTW','334'],['NGNCCNGT','INSTTTNGSNGNGNGT','AATCICGGSNGSECTW','234'\\\n",
    "]],columns = ['Cdr1','Cdr2','Cdr3','Counts'])\n",
    "    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       Cdr1              Cdr2              Cdr3 Counts\n",
      "0  GFTFSRYS          ITSSSRYI  CARDPACGDYVYFDYW    771\n",
      "1   NGNGCCG  ITTTTTNGSNGNGNGT   AATCITGSNGSECTW    589\n",
      "2   NGNCCGT  ITTTTTNGSNGNGNGT  AATCITGWGNGSECTW    334\n",
      "3  NGNCCNGT  INSTTTNGSNGNGNGT  AATCICGGSNGSECTW    234\n"
     ]
    }
   ],
   "source": [
    "    print(df_merge_clone)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Cdr1</th>\n",
       "      <th>Cdr2</th>\n",
       "      <th>Cdr3</th>\n",
       "      <th>Counts</th>\n",
       "      <th>seqlen</th>\n",
       "      <th>Cnum</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>GFTFSRYS</td>\n",
       "      <td>ITSSSRYI</td>\n",
       "      <td>CARDPACGDYVYFDYW</td>\n",
       "      <td>771</td>\n",
       "      <td>16</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NGNGCCG</td>\n",
       "      <td>ITTTTTNGSNGNGNGT</td>\n",
       "      <td>AATCITGSNGSECTW</td>\n",
       "      <td>589</td>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NGNCCGT</td>\n",
       "      <td>ITTTTTNGSNGNGNGT</td>\n",
       "      <td>AATCITGWGNGSECTW</td>\n",
       "      <td>334</td>\n",
       "      <td>16</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NGNCCNGT</td>\n",
       "      <td>INSTTTNGSNGNGNGT</td>\n",
       "      <td>AATCICGGSNGSECTW</td>\n",
       "      <td>234</td>\n",
       "      <td>16</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Cdr1              Cdr2              Cdr3 Counts  seqlen  Cnum\n",
       "0  GFTFSRYS          ITSSSRYI  CARDPACGDYVYFDYW    771      16     2\n",
       "1   NGNGCCG  ITTTTTNGSNGNGNGT   AATCITGSNGSECTW    589      15     2\n",
       "2   NGNCCGT  ITTTTTNGSNGNGNGT  AATCITGWGNGSECTW    334      16     2\n",
       "3  NGNCCNGT  INSTTTNGSNGNGNGT  AATCICGGSNGSECTW    234      16     3"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "    df_merge_clone['seqlen']=df_merge_clone['Cdr3'].map(lambda x : len(x))\n",
    "    df_merge_clone['Cnum']=df_merge_clone['Cdr3'].map(lambda x : x.count('C'))\n",
    "    df_merge_clone"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    #df_merge_clone['seqlen'].value_counts()\n",
    "\n",
    "    #df_merge_clone.loc[df_merge_clone['seqlen'].idxmax()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Cdr1</th>\n",
       "      <th>Cdr2</th>\n",
       "      <th>Cdr3</th>\n",
       "      <th>Counts</th>\n",
       "      <th>seqlen</th>\n",
       "      <th>Cnum</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>GFTFSRYS</td>\n",
       "      <td>ITSSSRYI</td>\n",
       "      <td>CARDPACGDYVYFDYW</td>\n",
       "      <td>771</td>\n",
       "      <td>16</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NGNCCGT</td>\n",
       "      <td>ITTTTTNGSNGNGNGT</td>\n",
       "      <td>AATCITGWGNGSECTW</td>\n",
       "      <td>334</td>\n",
       "      <td>16</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Cdr1              Cdr2              Cdr3 Counts  seqlen  Cnum\n",
       "0  GFTFSRYS          ITSSSRYI  CARDPACGDYVYFDYW    771      16     2\n",
       "2   NGNCCGT  ITTTTTNGSNGNGNGT  AATCITGWGNGSECTW    334      16     2"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''https://stackoverflow.com/questions/15741759/find-maximum-value-of-a-column-and-return-the-corresponding-row-values-using-pan'''\n",
    "df_clone_filter = df_merge_clone[(df_merge_clone['seqlen']==df_merge_clone['seqlen'].max()) & (df_merge_clone['Cnum']==2)]\n",
    "df_clone_filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    CARDPACGDYVYFDYW\n",
       "2    AATCITGWGNGSECTW\n",
       "Name: Cdr3, dtype: object"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_clone_filter['Cdr3']\n",
    "\n",
    "#df_clone_filter['Cdr3'].str.split('',expand=True).to_dict('index')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>C</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>R</td>\n",
       "      <td>T</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>D</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>P</td>\n",
       "      <td>I</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>A</td>\n",
       "      <td>T</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>C</td>\n",
       "      <td>G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>G</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>D</td>\n",
       "      <td>G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Y</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>V</td>\n",
       "      <td>G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Y</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>F</td>\n",
       "      <td>E</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>D</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>Y</td>\n",
       "      <td>T</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>W</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0  2\n",
       "0       \n",
       "1   C  A\n",
       "2   A  A\n",
       "3   R  T\n",
       "4   D  C\n",
       "5   P  I\n",
       "6   A  T\n",
       "7   C  G\n",
       "8   G  W\n",
       "9   D  G\n",
       "10  Y  N\n",
       "11  V  G\n",
       "12  Y  S\n",
       "13  F  E\n",
       "14  D  C\n",
       "15  Y  T\n",
       "16  W  W\n",
       "17      "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#转置行与列\n",
    "df=df_clone_filter['Cdr3'].str.split('',expand=True).T\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "2    2\n",
       "3    3\n",
       "Name: Cdr3, dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#df_clone_filter\n",
    "df_clone_filter['Cdr3'].map(lambda x : x.count('C'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Cdr1</th>\n",
       "      <th>Cdr2</th>\n",
       "      <th>Cdr3</th>\n",
       "      <th>Counts</th>\n",
       "      <th>seqlen</th>\n",
       "      <th>cdr1ptm</th>\n",
       "      <th>cdr2ptm</th>\n",
       "      <th>cdr3ptm</th>\n",
       "      <th>wseqptm</th>\n",
       "      <th>allptm</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>GFTFSRYS</td>\n",
       "      <td>ITSSSRYI</td>\n",
       "      <td>CARDPAYGDYVYFDYW</td>\n",
       "      <td>771</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NGNGCCG</td>\n",
       "      <td>ITTTTTNGSNGNGNGT</td>\n",
       "      <td>AATCITGCGSNGSECTW</td>\n",
       "      <td>589</td>\n",
       "      <td>17</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NGNCCGT</td>\n",
       "      <td>ITTTTTNGSNGNGNGT</td>\n",
       "      <td>AATCITGCGSNGSECTW</td>\n",
       "      <td>334</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NGNCCNGT</td>\n",
       "      <td>INSTTTNGSNGNGNGT</td>\n",
       "      <td>AATCICGCGSNGSECTW</td>\n",
       "      <td>234</td>\n",
       "      <td>17</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Cdr1              Cdr2               Cdr3 Counts  seqlen  cdr1ptm  \\\n",
       "0  GFTFSRYS          ITSSSRYI   CARDPAYGDYVYFDYW    771      16        0   \n",
       "1   NGNGCCG  ITTTTTNGSNGNGNGT  AATCITGCGSNGSECTW    589      17        2   \n",
       "2   NGNCCGT  ITTTTTNGSNGNGNGT  AATCITGCGSNGSECTW    334      17        1   \n",
       "3  NGNCCNGT  INSTTTNGSNGNGNGT  AATCICGCGSNGSECTW    234      17        2   \n",
       "\n",
       "   cdr2ptm  cdr3ptm  wseqptm  allptm  \n",
       "0        0        0        0       0  \n",
       "1        4        1        0       7  \n",
       "2        4        1        0       6  \n",
       "3        5        1        0       8  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "df_merge_clone['cdr1ptm'],df_merge_clone['cdr2ptm'],df_merge_clone['cdr3ptm'],df_merge_clone['wseqptm'],df_merge_clone['allptm']=zip(*df_merge_clone.apply(cal_ptms,axis=1))\n",
    "\n",
    "df_merge_clone"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
